/*
 * Copyright (c) 2010, 2012 Luan Nguyen Thanh. All rights reserved.
 * 
 * This code is written by Luan Nguyen Thanh, with any partners involved.
 * The use of this code should not be permitted!
 * If you want to use, please do contact me!
 */
package MyIM.client.view.form;

import MyIM.client.engine.Engine;
import MyIM.client.view.component.FriendListItem;
import MyIM.client.view.lnf.MetroStyle;
import MyIM.component.Friend;
import MyIM.component.Group;
import MyIM.utilities.Status;
import MyIM.utilities.Util;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Luan Nguyen Thanh <tklarryonline@gmail.com>
 */
public class Dialog_InviteConference extends JDialog {

    private Engine engine;
    private Dialog_Conference dialog_Conference;

    public Dialog_InviteConference(Engine engine,
            Dialog_Conference dialog_Conference) {
        this.engine = engine;
        this.dialog_Conference = dialog_Conference;

        init();
    }

    private void init() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

        initLayout();
        pack();
        setResizable(false);
        setLocationRelativeTo(null);
    }

    private void initLayout() {
        // Sets layout
        setLayout(new BorderLayout(5, 5));

        JLabel label = new JLabel("Invite someone?");
        label.setFont(MetroStyle.FORM_TITLE);
        label.setForeground(MetroStyle.LIGHTER_MARINE);
        add(label, BorderLayout.NORTH);

        add(createJTree());
    }

    public JTree createJTree() {
        ImageIcon ava = Util.makeIcon("contact");
        // Creates JTree
        final JTree jTree = new JTree();
        jTree.setRootVisible(false);
        jTree.putClientProperty("JTree.lineStyle", "Horizontal");
        FriendListItem renderer = new FriendListItem();

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
                "root node, should be invisible");
        DefaultTreeModel defaultTreeModel = new DefaultTreeModel(rootNode);

        jTree.setModel(defaultTreeModel);
        jTree.setCellRenderer(renderer);

        // Adds nodes to it
        DefaultMutableTreeNode parentNode;
        DefaultMutableTreeNode node;
        ArrayList<Group> groupList = engine.getClient().getFriendList();
        for (Group group : groupList) {
            System.out.println("Group Name: " + group.getGroupName());
            parentNode = (DefaultMutableTreeNode) defaultTreeModel.getRoot();
            node = new DefaultMutableTreeNode(group);
            addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);

            parentNode = node;
            ArrayList<Friend> friendInGroup = group.getFriends();
            for (Friend friend : friendInGroup) {
                node = new DefaultMutableTreeNode(friend);
                addNodeToDefaultTreeModel(defaultTreeModel, parentNode, node);
            }
        }

        expandAll(jTree);

        MouseListener ml = new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                int row = jTree.getRowForLocation(e.getX(), e.getY());
                TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
                if (row != -1 && path != null) {
                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                    Object userObj = node.getUserObject();
                    if (userObj instanceof Friend) {
                        Friend friendObj = (Friend) userObj;
                        System.out.println("Object Friend " + friendObj.getDisplayName());
                        if (SwingUtilities.isLeftMouseButton(e)) {
                            if (e.getClickCount() == 2) {
                                addAttendee(friendObj);
                            }
                        } else if (SwingUtilities.isRightMouseButton(e)) {
                        }
                    } else if (userObj instanceof Group) {
                        Group groupObj = (Group) userObj;
                        System.out.println("Object Group " + groupObj.getGroupName());
                    }
                }
            }
        };

        jTree.addMouseListener(ml);

        return jTree;
    }

    private void addNodeToDefaultTreeModel(DefaultTreeModel treeModel,
            DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node) {

        treeModel.insertNodeInto(node, parentNode, parentNode.getChildCount());

        if (parentNode == treeModel.getRoot()) {
            treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());
        }
    }

    private void addAttendee(Friend friendObj) {
        engine.inviteConference(dialog_Conference.getOwnerid(), dialog_Conference.getConferenceName(), dialog_Conference.getAttendees(), friendObj.getUid());
    }

    public void expandAll(JTree tree) {
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }
}
